package com.example.week04_work2;

import java.util.concurrent.CompletableFuture;

/**
 * @author nhsoft.ll
 */
public class CompletableFutureDemo {
    public static void main(String[] args) throws Exception{
        long start = System.currentTimeMillis();
        CompletableFutureSumTask completableFutureSumTask = new CompletableFutureSumTask();
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            completableFutureSumTask.run();
            return completableFutureSumTask.getResult();
        });
        System.out.println("异步计算结果为：" + completableFuture.get());
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

    }
}


class CompletableFutureSumTask {

    private int result;

    public int getResult() {
        return result;
    }

    public void run() {
        result = sum();
        System.out.println("线程运行结果:" + result);
    }

    private int sum() {
        return fibo(36);
    }

    private int fibo(int a) {
        if (a < 2)
            return 1;
        return fibo(a - 1) + fibo(a - 2);
    }
}
